SEEK Statement ---------------------------------------------------------------------------- Action Sets the position in a file for the next read or write. Syntax SEEK # filenumber%, position& Remarks The SEEK statement uses the following arguments. ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- filenumber% The number used in the OPEN statement to open the file. position& A numeric expression that indicates where the next read or write occurs. The position& value must be between 1 and 2,147,483,647 (equivalent to 231 -1), inclusive. For files opened in random-access mode, position& is the number of a record in the file. For files opened in binary, input, output, or append mode, position& is the byte position relative to the beginning of the file. The first byte in a file is at position 1; the second byte is at position 2, and so on. After a SEEK operation, the next file I-O operation starts at the specified byte position. Note Record numbers on a GET or PUT override the file positioning done by SEEK. Performing a file write after doing a SEEK operation beyond the end of a file extends the file. If you attempt a SEEK operation to a negative or zero position, BASIC generates the error message Bad record number. BASIC leaves the file position unchanged when you use SEEK on an ISAM table or on BASIC devices (SCRN, CONS, KYBD, COM n, LPT n, PIPE) that do not support SEEK. See Also GET (File I-O); OPEN; PUT (File I-O); SEEK Function; SEEKGT, SEEKGE, SEEKEQ Statements Example The following example uses a combination of the SEEK function and SEEK statement to move the file position exactly one record back and rewrite the record if a variable is true (nonzero). CONST FALSE = 0, TRUE = NOT FALSE ' Define record fields and a user-type variable. TYPE TestRecord NameField AS STRING * 20 ScoreField AS SINGLE END TYPE DIM RecordVar AS TestRecord DIM I AS LONG ' This part of the program creates the random-access file used by the ' second part of the program, which demonstrates the SEEK statement. OPEN "TESTDAT2.DAT" FOR RANDOM AS #1 LEN = LEN(RecordVar) RESTORE READ NameField$, ScoreField I = 0 DO WHILE NameField$ <> "END" I = I + 1 RecordVar.NameField = NameField$ RecordVar.ScoreField = ScoreField PUT #1, I, RecordVar READ NameField$, ScoreField LOOP CLOSE #1 DATA "John Simmons", 100 DATA "Allie Simpson", 95 DATA "Tom Tucker", 72 DATA "Walt Wagner", 90 DATA "Mel Zucker", 92 DATA "END", 0 ' Open the test data file. DIM FileBuffer AS TestRecord OPEN "TESTDAT2.DAT" FOR RANDOM AS #1 LEN = LEN(FileBuffer) ' Calculate number of records in the file. Max = LOF(1) \ LEN(FileBuffer) ' Read and print contents of each record. FOR I = 1 TO Max GET #1, I, FileBuffer IF FileBuffer.NameField = "Tom Tucker" THEN ReWriteFlag = TRUE EXIT FOR END IF NEXT I IF ReWriteFlag = TRUE THEN ' Back up file by the length of the record variable that ' is used to write to the file. FileBuffer.ScoreField = 100 SEEK #1, SEEK(1) - LEN(RecordVar) PUT #1, , RecordVar END IF CLOSE #1 KILL "TESTDAT2.DAT" END